using System.Web.Mvc;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Core.DomainModel;
using System.Collections.Generic;
using SharpArch.Web.NHibernate;
using NHibernate.Validator.Engine;
using System.Text;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;
using GoldenBicycleCompany.Core;
using GoldenBicycleCompany.ApplicationServices;
using GoldenBicycleCompany.ApplicationServices.ViewModels;
using GoldenBicycleCompany.Core.QueryDtos;
using GoldenBicycleCompany.Core.RepositoryInterfaces;
 

namespace GoldenBicycleCompany.Web.Controllers
{
    [HandleError]
    public class BicyclesController : Controller
    {
        public BicyclesController(IBicycleManagementService bicycleManagementService) {
            Check.Require(bicycleManagementService != null, "bicycleManagementService may not be null");

            this.bicycleManagementService = bicycleManagementService;
        }

        [Transaction]
        public ActionResult Index() {
            IList<BicycleDto> bicycles = 
                bicycleManagementService.GetBicycleSummaries();
            return View(bicycles);
        }

        [Transaction]
        public ActionResult Show(int id) {
            Bicycle bicycle = bicycleManagementService.Get(id);
            return View(bicycle);
        }

        [Transaction]
        public ActionResult Create() {
            BicycleFormViewModel viewModel = 
                bicycleManagementService.CreateFormViewModel();
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Bicycle bicycle) {
            if (ViewData.ModelState.IsValid) {
                ActionConfirmation saveOrUpdateConfirmation = 
                    bicycleManagementService.SaveOrUpdate(bicycle);

                if (saveOrUpdateConfirmation.WasSuccessful) {
                    TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
                        saveOrUpdateConfirmation.Message;
                    return RedirectToAction("Index");
                }
            } else {
                bicycle = null;
            }

            BicycleFormViewModel viewModel = 
                bicycleManagementService.CreateFormViewModelFor(bicycle);
            return View(viewModel);
        }

        [Transaction]
        public ActionResult Edit(int id) {
            BicycleFormViewModel viewModel = 
                bicycleManagementService.CreateFormViewModelFor(id);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Bicycle bicycle) {
            if (ViewData.ModelState.IsValid) {
                ActionConfirmation updateConfirmation = 
                    bicycleManagementService.UpdateWith(bicycle, bicycle.Id);

                if (updateConfirmation.WasSuccessful) {
                    TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
                        updateConfirmation.Message;
                    return RedirectToAction("Index");
                }
            }

            BicycleFormViewModel viewModel = 
                bicycleManagementService.CreateFormViewModelFor(bicycle);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            ActionConfirmation deleteConfirmation = bicycleManagementService.Delete(id);
            TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
                deleteConfirmation.Message;
            return RedirectToAction("Index");
        }

        private readonly IBicycleManagementService bicycleManagementService;
    }
}
